package com.yyj.shoppingOnLine.common.util;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * 加密(采用sha256)
 * @author Administrator
 *
 */
public class EncryptUtil
{
	/**
	 * sha256$radNum$加密(sha266$radNum$明文)
	 * @param pass:明文
	 * @return 密文
	 */
	public static String encodePass(String pass)
	{
		String encType="sha256";
		String radNum=RegexUtil.proccedStr(32);
		/*加密【前置】字符串*/
		String encStr=encType+"$"+radNum+"$";
		/*加密前字符串*/
		String encHead=encStr+pass;
		/*加密后字符串*/
		String encBack=DigestUtils.sha256Hex(encHead);
		return encStr+encBack;
	}
	
	/**
	 * @param souPass 原密码
	 * @param encPass 加密后密码
	 * @return 输入的密码是否与数据库密码一致
	 */
	public static boolean checkPass(String souPass, String encPass)
	{
		String[] arrStr=encPass.split("\\$");
		String encType=arrStr[0];
		String radNum=arrStr[1];
		/*加密【前置】字符串*/
		String encStr=encType+"$"+radNum+"$";
		/*加密前字符串*/
		String encHead=encStr+souPass;
		/*加密后字符串*/
		String encBack=DigestUtils.sha256Hex(encHead);
		if(encBack.equals(arrStr[2]))
		{
			return true;
		}
		return false;
	}
	
	public static void main(String[] args)
	{
		String souPass="111111";
		String pass=EncryptUtil.encodePass(souPass);
		ConstatFinalUtil.LOG.info(pass);
		String encPass="";
		boolean bo=EncryptUtil.checkPass(souPass, encPass);
		ConstatFinalUtil.LOG.info(bo);
	}
}
